const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatar: '',
    nickname: '',
  },
  handleSubmit() {
    const postData = this.data;
    if (!postData.avatar) {
      wx.showModal({
        title: '温馨提示',
        content: '头像不能为空！',
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
      return
    }
    if (!postData.nickname) {
      wx.showModal({
        title: '温馨提示',
        content: '昵称不能为空！',
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
      return
    }
    util.https('user/baseInfoMod', { avatar: postData.avatar, nickname: postData.nickname }, (res) => {
      console.log(res)
      let user = wx.getStorageSync('user');
      user.nickname = postData.nickname;
      user.avatar = postData.avatar;
      wx.setStorageSync('user', user)
      wx.navigateBack({
        delta: 1
      })
      wx.showToast({
        title: '保存成功',
        icon: 'success',
        duration: 5000
      })
    }, (res) => {
      console.log(res)
      wx.showModal({
        title: '温馨提示',
        content: '保存失败',
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
    }, () => {
    })
  },
  doUpLoadImage(e) {
    const _this = this;
    const data = e.currentTarget.dataset
    console.log(data)
    wx.chooseImage({
      count: 1,
      success(res) {
        console.log(res)
        wx.getFileSystemManager().readFile({
          filePath: res.tempFilePaths[0],
          encoding: "base64",
          success(res2) {
            console.log(res2)
            // let sss = 'data:image/png;base64,' + res2.data
            const sss = res2.data
            wx.showLoading({
              title: '文件上传中...',
            })
            util.https('comm/imageUpload', { imageBase64: sss }, (res) => {
              console.log(res)
              _this.setData({
                avatar: res.imageUrl
              })
              wx.showToast({
                title: '上传成功',
                icon: 'success',
                duration: 5000
              })
            }, (res) => {
              console.log(res)
              wx.showModal({
                title: '温馨提示',
                content: '上传失败',
                confirmColor: '#F08302',
                showCancel: false,
                success(res) {
                }
              })
            }, () => {
              wx.hideLoading()
            })
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let user = wx.getStorageSync('user');
    if (user.nickname) {
      this.setData({
        nickname: user.nickname
      })
    }
    if (user.avatar) {
      this.setData({
        avatar: user.avatar
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  }
  
})